package cn.jiyun.controller;


import cn.jiyun.client.UserClient;
import cn.jiyun.pojo.Order;
import cn.jiyun.pojo.User;
import cn.jiyun.service.Oservice;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("order")
public class Ocontroller {

    @Autowired
    private Oservice oservice;

//    @Autowired
//    private RestTemplate restTemplate;
    @Autowired
    private UserClient userClient;




    @SentinelResource("hot")
    @GetMapping("hx")
    public Order all( Long id,@RequestHeader(required = false) String Truth){
        Order all = oservice.all(id);
//        String url="http://userservice/user/"+all.getUserId();
//        User user = restTemplate.getForObject(url, User.class);
        User user=userClient.findByid(all.getUserId());
        all.setUser(user);
        return  all;
    }

    @GetMapping("xlk")
    public List<User> xlk(){
        return userClient.all();
    }


    @GetMapping("allqc")
    public Page<Order> allqc(Integer page,Integer pageSize,String input ){
        Page<Order> page1=new Page<>(page,pageSize);
       Page<Order> qc=oservice.qc(page1,input);
        for (Order order : qc.getRecords()) {
            User user=userClient.findByid(order.getUserId());
            order.setUser(user);
        }
//        String url="http://userservice/user/"+all.getUserId();
//        User user = restTemplate.getForObject(url, User.class);

        return  qc;
    }

    @GetMapping("del")
    public void del(Long id){
        oservice.del(id);
//        userClient.del(id);
    }

    @PostMapping("add")
    public void add(@RequestBody Order order){
        oservice.add(order);
    }

    @PostMapping("update")
    public void update(@RequestBody Order order){
        oservice.update(order);
    }

}
